package buildings.dwelling.hotel;

import buildings.Building;
import buildings.Space;
import buildings.Floor;
import buildings.dwelling.*;

public class Hotel extends Dwelling implements Building, Cloneable {

    public Hotel(Floor[] floors) {
        super(floors);
    }

    public Hotel(int floorCount, int[] roomCounts) {
        super(new Floor[floorCount]);
        buildings.Buildings.setBuildingFactory(new HotelFactory());
        for(int i = 0; i < floorCount; i++) {
            super.setFloorByNumber(i,
                    buildings.Buildings.createFloor(roomCounts[i]));
        }
        
    }

    public int getStars() {
        Floor[] floors = super.getFloors();
        int stars = 0;
        for (int i = 0; i < floors.length; i++) {
            if (!(floors[i] instanceof HotelFloor)) {
                continue;
            }
            if (((HotelFloor) floors[i]).getStars() > stars) {
                stars = ((HotelFloor) floors[i]).getStars();
            }
        }
        return stars;
    }

    public Space getBestSpace() {
        Floor[] floors = super.getFloors();
        double p = 0;
        double maxP = 0;                                //лучший коэффициент      
        Space bestSpace = floors[0].getBestSpace();     //лучший номер
        for (int i = 0; i < floors.length; i++) {
            if (!(floors[i] instanceof HotelFloor)) {
                continue;
            }
            switch(((HotelFloor)floors[i]).getStars()) { //!!!!
                case 1: p = floors[i].getBestSpace().getSquare() * 0.25; break;
                case 2: p = floors[i].getBestSpace().getSquare() * 0.5; break;
                case 3: p = floors[i].getBestSpace().getSquare(); break;
                case 4: p = floors[i].getBestSpace().getSquare() * 1.25; break;
                case 5: p = floors[i].getBestSpace().getSquare() * 1.5; break;
            }
            if(p > maxP) {
                maxP = p;
                bestSpace = floors[i].getBestSpace();
            }         
        }
        return bestSpace;
    }

    public String toString() {
        StringBuffer buf = new StringBuffer(this.getClass().getSimpleName() 
                + " (" + getStars() + ", " + getFloorCount());
        for(int i = 0; i < getFloorCount(); i++) {
            buf.append(" ");
            buf.append(getFloorByNumber(i));
        }
        buf.append(")");
        return buf.toString();
    }
    
    public boolean equals(Object obj) {
        if(!(obj instanceof Hotel ))
            return false;
        boolean isFloorsEquals = true;
        for(int i = 0; i < getFloorCount(); i++) {
            if(!((Hotel)obj).getFloorByNumber(i).equals(getFloorByNumber(i))) {
                isFloorsEquals = false;
                break;
            }
        }
        return((((Hotel)obj).getStars() == getStars())
                && (((Hotel)obj).getFloorCount() == getFloorCount())
                && (isFloorsEquals));
    }
    
    public int hashCode() {
        int hash = getStars() | getSpaceCount();
        for(int i = 0; i < getFloorCount(); i++) {
            hash |= getFloorByNumber(i).hashCode();
        }
        return hash;
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
